﻿using Project.API.Models;
using Project.API.Models.information;
using Project.BLL.information;
using Project.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Project.API.Controllers.information
{
    public class storesController : ApiController
    {
        // GET: api/stores
        [HttpGet]
        public ResultPageList<storeDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            var list = new storeBll().GetStoresByPage(page, limit, name, out count);
            var data = from u in list
                       select new storeDto
                       {
                           store_id = u.store_id,
                           store_name = u.store_name,
                           store_address = u.store_address,
                           store_number = u.store_number,
                           store_text = u.store_text,
                       };

            ResultPageList<storeDto> result = new ResultPageList<storeDto>();
            result.code = 0;
            result.msg = "ok";
            result.count = count;
            result.data = data.ToList();

            return result;
        }

        // POST: api/stores
        public Result Post([FromBody]storeDto value)
        {
            store sto = new store();
            sto.store_name = value.store_name;
            sto.store_address=value.store_address;
            sto.store_number= value.store_number;
            sto.store_text= value.store_text;

            bool bol=new storeBll().Add(sto);

            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息添加成功！" : "error";
            return result;


        }

        // PUT: api/stores/5
        public Result Put( [FromBody]storeDto value)
        {
            store sto = new store();
            sto.store_id=value.store_id;    
            sto.store_name=value.store_name;
            sto.store_number = value.store_number;
            sto.store_text = value.store_text;
            sto.store_address= value.store_address;

            bool bol = new storeBll().Update(sto);
            Result result= new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息修改成功！" : "error";
            return result;


        }

        // DELETE: api/stores/5
        public Result Delete(int id)
        {
            bool bol = new storeBll().Del(id);
            Result result = new Result();
            result .code = bol ? 1 : 0;
            result.msg = bol ? "用户删除成功" : "error";
            return result;
        }
        [HttpDelete]
        public Result Delete(int[] ids)
        {
            bool bol=new storeBll().Del(ids);
            Result result = new Result();
            result.code = bol ? 1 : 0;
            result.msg = bol ? "用户信息删除成功！" : "出错了！";
            return result;
        }
    }
}
